package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/08 14:43
 **/
public class Day6_7_Num129_SumNumbers {
    class Solution {
        int totalSum = 0;
        public int sumNumbers(TreeNode root) {
            dfs(root, 0);
            return totalSum;
        }
        private void dfs(TreeNode root, int pre){
            if(root == null) return;
            if(root.left == null && root.right == null) {
                totalSum+=(pre * 10 + root.val);
                return;
            }
            dfs(root.left, (pre * 10 + root.val));
            dfs(root.right, (pre * 10 + root.val));
        }
    }
}